#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QCloseEvent>
#include <QMenu>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    m_trayicon.setParent(this);
    m_trayicon.setIcon(QIcon(":/res/4451be2034d642d0b7f15ba105788264.png"));
    m_trayicon.show();

    auto traymenu = new QMenu();

    m_trayicon.setContextMenu([this]()
    {
        auto menu = new QMenu;
        menu->addAction("退出", [this]()
        {
            qApp->quit();
        });
        return menu;
    }
    ());
    connect(&m_trayicon, &QSystemTrayIcon::activated, this, [this](QSystemTrayIcon::ActivationReason reason)
    {
        switch (reason)
        {
            case QSystemTrayIcon::MiddleClick:
                break;
            case QSystemTrayIcon::Trigger:
                m_trayicon.setIcon(QIcon(":/res/4451be2034d642d0b7f15ba105788264.png"));
                this->show();
                break;
            case QSystemTrayIcon::DoubleClick: // 正常来说应该触发此信号但是没有
                m_trayicon.setIcon(QIcon(":/res/4451be2034d642d0b7f15ba105788264.png"));
                this->show();
                break;
            default:
                break;
        }
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::closeEvent(QCloseEvent *event)
{
    if (m_trayicon.isVisible())
    {
        hide();
        m_trayicon.showMessage("通知", "已最小化到托盘区域", QIcon(":/res/eaea36cab7964db4936c935a57118636.png"), 1500);
        event->ignore();
    }
    else
    {
        event->accept();
    }
}
